<template>
  <XtxBread>
    <XtxBreadItem to="/">首页</XtxBreadItem>
    <XtxBreadItem v-if="category.top" to="`/category/${category.top.id}`">{{category.top.name}}</XtxBreadItem>
    <Transition name="fade-right" mode="out-in">
      <XtxBreadItem :key="category.sub.id" v-if="category.sub">
        {{ category.sub.name }}
      </XtxBreadItem>
    </Transition>
  </XtxBread>
</template>
<script>
import { computed } from 'vue'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
export default {
  name: 'SubBread',
  setup () {
    const store = useStore()
    const route = useRoute()
    const category = computed(() => {
      const obj = {
        sub: {},
        top: {}
      }
      store.state.category.list.forEach(item => {
        item.children && item.children.forEach(sub => {
          if (sub.id === route.params.id) {
            obj.sub = sub
            obj.top = item
          }
        })
      })
      return obj
    })

    return {
      category
    }
  }
}
</script>
